<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>待办事项</title>
  <style>
    /* 保持原有样式不变 */
    body {
      font-family: "Microsoft YaHei", sans-serif;
      background-color: #f5f5f9;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 40px 20px; /* 优化移动端内边距 */
      margin: 0;
      min-height: 100vh;
      box-sizing: border-box;
    }
    .container {
      background: white;
      border-radius: 16px;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
      width: 100%;
      max-width: 480px;
      padding: 24px;
      box-sizing: border-box;
    }
    h1 {
      text-align: center;
      color: #4a4af5;
      font-size: 2rem;
      margin-bottom: 6px;
    }
    p.subtitle {
      text-align: center;
      color: #777;
      margin-bottom: 24px;
    }
    .input-area {
      display: flex;
      gap: 8px;
      margin-bottom: 20px;
    }
    input[type="text"] {
      flex: 1;
      padding: 12px;
      border-radius: 8px;
      border: 1px solid #ccc;
      font-size: 16px;
      outline: none;
      width: 100%;
      box-sizing: border-box;
    }
    button.add-btn {
      background: #4a4af5;
      color: #fff;
      border: none;
      padding: 0 18px;
      border-radius: 8px;
      font-size: 16px;
      cursor: pointer;
      transition: background 0.3s;
    }
    button.add-btn:hover {
      background: #3a3ad5;
    }
    .filters {
      display: flex;
      justify-content: flex-start;
      gap: 10px;
      margin-bottom: 16px;
      flex-wrap: wrap; /* 确保小屏幕下按钮不会溢出 */
    }
    .filters button {
      border: 1px solid #ccc;
      border-radius: 20px;
      background: #fff;
      padding: 6px 16px;
      cursor: pointer;
      transition: all 0.3s;
    }
    .filters button.active {
      background: #c5c5ee;
      color: #4a4af5;
      border-color: #4a4af5;
    }
    ul.todo-list {
      list-style: none;
      padding: 0;
      margin: 0;
    }

    li.todo-item {
      background: #fafafa;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 10px;
      padding: 12px;
      border-radius: 10px;
      box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
      opacity: 1;
      transition: all 0.3s ease;
    }

    li.todo-item.fade-out {
      opacity: 0;
      transform: translateX(30px);
    }

    .checkbox {
      width: 22px;
      height: 22px;
      border: 2px solid #aaa;
      border-radius: 6px;
      position: relative;
      cursor: pointer;
      flex-shrink: 0;
      transition: all 0.3s;
    }

    .checkbox.checked {
      background-color: #4a4af5;
      border-color: #4a4af5;
    }

    .checkbox::after {
      content: "";
      position: absolute;
      left: 5px;
      top: 1px;
      width: 6px;
      height: 12px;
      border-right: 3px solid white;
      border-bottom: 3px solid white;
      transform: scale(0) rotate(45deg);
      transition: transform 0.3s ease;
    }

    .checkbox.checked::after {
      transform: scale(1) rotate(45deg);
    }

    .todo-text {
      flex: 1;
      margin-left: 10px;
      font-size: 16px;
      color: #333;
      transition: color 0.3s, text-decoration 0.3s;
      word-break: break-word; /* 防止长文本溢出 */
    }

    .todo-text.completed {
      text-decoration: line-through;
      color: #999;
    }

    .delete-btn {
      background: none;
      border: none;
      color: #aaa;
      font-size: 18px;
      cursor: pointer;
      transition: color 0.3s;
      flex-shrink: 0;
    }

    .delete-btn:hover {
      color: #ff4d4f;
    }

    .summary {
      text-align: center;
      color: #666;
      margin-top: 16px;
      padding-top: 16px;
      border-top: 1px solid #eee;
    }
    @media (max-width: 480px) {
      h1 {
        font-size: 1.5rem;
      }
      button.add-btn {
        font-size: 14px;
        padding: 0 12px;
      }
      input[type="text"] {
        font-size: 14px;
      }
      .container {
        padding: 16px;
      }
    }
  </style>
</head>
<body>
  <h1>待办事项</h1>
  <p class="subtitle">简单高效地管理你的任务</p>

  <div class="container">
    <div class="input-area">
      <input type="text" id="todo-input" placeholder="添加新的待办事项…" />
      <button class="add-btn" id="add-btn">+添加</button>
    </div>

    <div class="filters">
      <button data-filter="all" class="active">全部</button>
      <button data-filter="active">未完成</button>
      <button data-filter="completed">已完成</button>
    </div>

    <ul class="todo-list" id="todo-list"></ul>

    <div class="summary" id="summary"></div>
  </div>

  <script>
    class TodoItem {
      /**
       * 待办事项类
       * @param {string} text - 待办事项内容
       * @param {boolean} completed - 是否完成
       */
      constructor(text, completed = false) {
        this.id = Date.now(); // 唯一ID
        this.text = text;     // 待办内容
        this.completed = completed; // 完成状态
        this.createdAt = new Date(); // 创建时间
      }
    }

    class TodoApp {
      constructor() {
        // 从本地存储加载数据，转换为TodoItem实例
        this.todos = (JSON.parse(localStorage.getItem("todos")) || [])
          .map(item => {
            const todo = new TodoItem(item.text, item.completed);
            todo.id = item.id; // 保持原有ID
            return todo;
          });
        this.filter = "all";
        this.listEl = document.getElementById("todo-list");
        this.inputEl = document.getElementById("todo-input");
        this.summaryEl = document.getElementById("summary");
        this.init();
      }

      init() {
        this.render();
        // 添加待办事项事件
        document.getElementById("add-btn").addEventListener("click", () => this.addTodo());
        this.inputEl.addEventListener("keypress", (e) => {
          if (e.key === "Enter") this.addTodo();
        });
        // 过滤按钮事件
        document.querySelectorAll(".filters button").forEach(btn => {
          btn.addEventListener("click", (e) => {
            document.querySelectorAll(".filters button").forEach(b => b.classList.remove("active"));
            e.target.classList.add("active");
            this.filter = e.target.dataset.filter;
            this.render();
          });
        });
      }

      // 添加新待办事项
      addTodo() {
        const text = this.inputEl.value.trim();
        if (!text) return;
        this.todos.push(new TodoItem(text));
        this.inputEl.value = "";
        this.save();
        this.render();
        // 添加后自动聚焦输入框
        this.inputEl.focus();
      }

      // 切换待办事项完成状态
      toggleTodo(id) {
        const todo = this.todos.find(t => t.id === id);
        if (todo) todo.completed = !todo.completed;
        this.save();
        this.render();
      }

      // 删除待办事项
      deleteTodo(id) {
        const itemEl = document.querySelector(`[data-id="${id}"]`);
        if (itemEl) {
          itemEl.classList.add("fade-out");
          setTimeout(() => {
            this.todos = this.todos.filter(t => t.id !== id);
            this.save();
            this.render();
          }, 300);
        }
      }

      // 保存到本地存储
      save() {
        // 只保存需要的数据，排除方法和不必要的属性
        const saveData = this.todos.map(todo => ({
          id: todo.id,
          text: todo.text,
          completed: todo.completed
        }));
        localStorage.setItem("todos", JSON.stringify(saveData));
      }

      // 获取过滤后的待办事项
      getFilteredTodos() {
        switch (this.filter) {
          case "active":
            return this.todos.filter(t => !t.completed);
          case "completed":
            return this.todos.filter(t => t.completed);
          default:
            return this.todos;
        }
      }

      // 渲染待办事项列表
      render() {
        this.listEl.innerHTML = "";
        const todos = this.getFilteredTodos();
        
        if (todos.length === 0) {
          const emptyMsg = document.createElement("li");
          emptyMsg.style.textAlign = "center";
          emptyMsg.style.padding = "20px";
          emptyMsg.style.color = "#777";
          emptyMsg.textContent = this.filter === "active" 
            ? "没有未完成的待办事项" 
            : this.filter === "completed" 
              ? "没有已完成的待办事项" 
              : "还没有待办事项，添加一个吧！";
          this.listEl.appendChild(emptyMsg);
        } else {
          todos.forEach(todo => {
            const li = document.createElement("li");
            li.className = "todo-item";
            li.dataset.id = todo.id;

            const checkbox = document.createElement("div");
            checkbox.className = "checkbox" + (todo.completed ? " checked" : "");
            checkbox.addEventListener("click", () => this.toggleTodo(todo.id));

            const text = document.createElement("span");
            text.className = "todo-text" + (todo.completed ? " completed" : "");
            text.textContent = todo.text;

            const delBtn = document.createElement("button");
            delBtn.className = "delete-btn";
            delBtn.innerHTML = "🗑️";
            delBtn.addEventListener("click", () => this.deleteTodo(todo.id));

            li.appendChild(checkbox);
            li.appendChild(text);
            li.appendChild(delBtn);
            this.listEl.appendChild(li);
          });
        }

        // 更新统计信息
        const total = this.todos.length;
        const undone = this.todos.filter(t => !t.completed).length;
        this.summaryEl.textContent = `${undone} 个未完成，${total} 个总计`;
      }
    }

    // 初始化应用
    document.addEventListener("DOMContentLoaded", () => {
      new TodoApp();
    });
  </script>
</body>
</html>